<template>
  <div ref="container">
    <a-modal
      :title="title"
      :width="500"
      :visible="visible"
      :confirm-loading="confirmLoading"
      :getContainer="() => $refs.container"
      :maskStyle="{ top: '93px', left: '154px' }"
      :wrapClassName="wrapClassNameInfo()"
      :mask="isDesktop()"
      :maskClosable="false"
      @ok="handleOk"
      @cancel="handleCancel"
      cancelText="取消"
      okText="保存"
      style="top: 30%; height: 45%"
    >
      <template slot="footer">
        <a-button key="back" v-if="isReadOnly" @click="handleCancel"> 取消 </a-button>
      </template>
      <a-spin :spinning="confirmLoading">
        <a-form :form="form">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="首个条码">
            <a-input placeholder="请输入第一个条码" v-decorator.trim="['barCodeVal', validatorRules.barCodeVal]" />
          </a-form-item>
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="条码数量">
            <a-input placeholder="请输入条码数量" v-decorator.trim="['count', validatorRules.count]" />
          </a-form-item>
        </a-form>
      </a-spin>
    </a-modal>
  </div>
</template>

<script>
import { mixinDevice } from '@/utils/mixin'
import { checkMaterialBarCode, getMaxBarCode } from '@/api/api'
export default {
  name: 'BatchSetPriceModal',
  mixins: [mixinDevice],
  data() {
    return {
      title: '批量设置',
      visible: false,
      isReadOnly: false,
      batchType: '',
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      form: this.$form.createForm(this),
      validatorRules: {
        barCodeVal: {
          rules: [
            { required: true, message: '请输入条码!' },
            { pattern: /^[1-9]\d{4,40}$/, message: '请输入正确的数字条码!' },
            { handler: this.validateBarCode },
          ],
        },
        count: {
          rules: [
            { required: true, message: '请输入数量!' },
            { pattern: /^\d*$/, message: '请输入正确的数量值!' },
          ],
        },
      },
    }
  },
  created() {},
  methods: {
    validateBarCode(type, value, row, column, callback, target) {
      let params = {
        barCode: value,
        id: 0,
      }
      checkMaterialBarCode(params).then((res) => {
        if (res && res.code === 200) {
          if (!res.data.status) {
            callback(true)
          } else {
            callback(false, '该条码已经存在')
          }
        } else {
          callback(false, res.data)
        }
      })
    },
    add() {
      this.edit({})
      getMaxBarCode({}).then((res) => {
        if (res && res.code === 200) {
          let maxBarCode = res.data.barCode - 0
          this.$nextTick(() => {
            this.form.setFieldsValue({ barCodeVal: (maxBarCode + 1) + ''})
          })
        }
      })
    },
    edit(record) {
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      this.form.validateFields((err, values) => {
        if (err) {
          this.$message.error('请填写所有必填项!')
        } else {
          let barCodeVal = this.form.getFieldValue('barCodeVal')
          let count = this.form.getFieldValue('count')

          this.$emit('ok', { barCodeVal, count })
          this.visible = false
        }
      })
    },
    handleCancel() {
      this.close()
    },
  },
}
</script>

<style scoped></style>
